package com.tsing.controller.admin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tsing.common.result.Result;
import com.tsing.service.AdUserService;
import com.tsing.service.impl.AdUserServiceImpl;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author 吴阳基
 * @datetime 2023/11/28 10:48
// */
//public class IndexController extends HttpServlet {
//
//    public Result login(HttpServletRequest request, HttpServletResponse response) {
//
//        Map<String, Object> map = new HashMap<>();
//        map.put("token", "1111111111");
//        map.put("name", "admin");
//        return Result.success(map);
//    }
//    public Result info(HttpServletRequest request, HttpServletResponse response) {
//        Map<String, Object> map = new HashMap<>();
//        map.put("roles","[admin]");
//        map.put("name","admin");
//        map.put("avatar","https://oss.aliyuncs.com/aliyun_id_photo_bucket/default_handsome.jpg");
//        return Result.success(map);
//    }
//    public Result logout(HttpServletRequest request, HttpServletResponse response){
//        return Result.success();
//    }
//}
public class AdUserController extends HttpServlet {

    // 模拟数据库用户表
    private AdUserService adUserService = new AdUserServiceImpl();

    public Result login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        // 获取前端传来的用户名、密码
//        String username = request.getParameter("username");
//        String password = request.getParameter("password");

        // 获取请求体数据
        BufferedReader reader = request.getReader();
        StringBuilder jsonRequest = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonRequest.append(line);
        }

        JSONObject jsonObject = JSON.parseObject(jsonRequest.toString());
        String username = (String) jsonObject.get("name");
        String password = (String) jsonObject.get("password");


        Integer i = adUserService.authenticateUser(username, password);

        if(i == 4) {
            return Result.error("该用户不存在");
        }else if (i  == 2) {
            return Result.error("加密算法错误");
        }else if (i == 3) {
            return Result.error("密码错误");
        }

        // 登录成功,返回token,可使用 UUID 或 JWT 生成
        String token = UUID.randomUUID().toString();

        Map<String, Object> userInfo = new HashMap<>();
        userInfo.put("token", token);
        userInfo.put("name", username);
        userInfo.put("photo", "https://oss.aliyuncs.com/aliyun_id_photo_bucket/default_handsome.jpg");

        return Result.success(userInfo);
    }

    public Result info(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");


        Map<String, Object> map = new HashMap<>();
        map.put("roles","[admin]");
        map.put("name","admin");
        map.put("avatar","https://oss.aliyuncs.com/aliyun_id_photo_bucket/default_handsome.jpg");
        return Result.success(map);
    }
    public Result logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");

        
        return Result.success();
    }
}